package com.longjianghuasheng.logistics.business.detail

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import com.longjianghuasheng.logistics.base.BaseMultiStateViewModel
import com.longjianghuasheng.logistics.data.model.bean.ApiResponseNoData
import com.longjianghuasheng.logistics.entity.StorageInfoResponse
import com.longjianghuasheng.logistics.ext.commit
import com.longjianghuasheng.logistics.ext.commit2
import com.longjianghuasheng.logistics.ext.judge
import com.longjianghuasheng.logistics.ext.toastNormal
import com.longjianghuasheng.logistics.network.apiService

class WarehousingViewModel : BaseMultiStateViewModel() {

    var mobile = ""

    var address = ""

    var id = 0

    var sid1 = ""

    var sid2 = ""

    var goods_name = ""

    var times = ""

    var mianji = ""

    private val result by lazy {
        MutableLiveData<StorageInfoResponse>()
    }

    val notify by lazy {
        MutableLiveData<ApiResponseNoData>()
    }

    val storage_son_result by lazy {
        MutableLiveData<ArrayList<WarehousingEntity>>()
    }
    val convertResult = result.map { storage ->


        storage.storage?.forEach {


            it.list = storage.storage_son
        }


        storage

    }


    fun storage_info() {
        getData2({ apiService.storage_info(id) }, result)

    }

    fun storage_son(id: Int) {

        commit({ apiService.storage_son(id) }, storage_son_result)
    }


    fun service_form() {

        var isSuccess = false
        when {

            sid2.judge("请选择库位") -> {}
            goods_name.judge("请输入货物名称") -> {}
            times.judge("请选择租赁时间") -> {}
            mianji.judge("请选择租赁面积") -> {}

            else -> isSuccess = true
        }

        if (!isSuccess) return


        val map = hashMapOf<String, String>().apply {
            put("service_id", id.toString())
            put("sid1", sid1)
            put("sid2", sid2)

            put("goods_name", goods_name)

            put("times", times)

            put("mianji", mianji)

        }
        commit2({ apiService.service_form(map) }, notify)
    }
}